class RangeParser {
  constructor (ranges) {
    this.rangesMap = ranges.split(',').map((range) => {
      let rangeRegExp = new RegExp(`(\\d+)-(\\d+)`)

      return {
        text: range,
        test: (val) => {
          let match
          if (match = range.match(rangeRegExp)) {
            return val >= +match[1] && val <= +match[2]
          }
          if (/以下|小于/.test(range)) {
            return val < range.match(/\d+/)
          }
          if (/以上|大于/.test(range)) {
            return val > range.match(/\d+/)
          }
        }
      }
    })
  }
  range (val) {
    for (let i = 0; i < this.rangesMap.length; i++) {
      let item = this.rangesMap[i]
      if (item.test(+val)) {
        return item.text
      }
    }
    return null
  }
}

module.exports = RangeParser